package com.itheima.tcp;

import java.io.*;
import java.net.Socket;

public class ThreadRuned implements Runnable {
    private Socket accept;

    public ThreadRuned(Socket accept) {
        this.accept = accept;
    }

    @Override
    public void run() {
        //本地IO流，数据永久化存储
        BufferedOutputStream bu = null;
        try {
            BufferedInputStream bis = new BufferedInputStream(accept.getInputStream());
            // System.currentTimeMillis()+".后缀名"/UUID.randerUUID.toString()+".后缀名"
            bu = new BufferedOutputStream(new FileOutputStream("day14\\Service\\copy.PNG" + System.currentTimeMillis() + ".PNG"));
            int b;
            while ((b = bis.read()) != -1) {
                bu.write(b);
            }
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bufferedWriter.write("成功");
            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (accept != null) {
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bu != null) {
                try {
                    bu.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
